const {Respones,DALIYCHAType} = require("../../config/gameCfg");
const gameCfg = require('../../config/gameCfg');
const redisBase = require('../../common/RedisBase');
const PlayerInfoZt = require('./playerInfoZt');
const log = require('../../utils/log');
const schedule = require('node-schedule');

//每日挑战大厅
class HallTz {
    constructor() {
        //休闲消耗
        this.costXX = 0;
        //大神消耗
        this.costDS = 300;

        //抽奖券，每天3次
        this.lastQaun = 3;
        //今日剩余挑战次数
        this.lastCount = 5;
        //每日看视频次数
        this.seeCount = 3;

        this.gameCfg = [
            {
                total: 15,
                rewards:[
                    {topicNum: 5, rewardType:gameCfg.RewardType.Type_Quan,rewardNum:2},
                    {topicNum: 10, rewardType:gameCfg.RewardType.Type_Quan,rewardNum:2},
                    {topicNum: 15, rewardType:gameCfg.RewardType.Type_Quan,rewardNum:6}
                ]
            },
            {
                total: 24,
                rewards:[
                    {topicNum: 8, rewardType:gameCfg.RewardType.Type_Quan,rewardNum:5},
                    {topicNum: 16, rewardType:gameCfg.RewardType.Type_Quan,rewardNum:5},
                    {topicNum: 24, rewardType:gameCfg.RewardType.Type_Quan,rewardNum:15}
                ]
            }
        ]

        // ：在每天的23:resetDate 重置部分数据
        this.scheduleFunction('59 23 * * *');
    }

    // 设置定时任务
    scheduleFunction(time) {
        // 使用node-schedule库创建定时任务
        const job = schedule.scheduleJob(time, () => {
            // 在指定时间执行函数
            this.resetDate();
        });
    }

    //定时执行
    async resetDate(){
        log.info('==========resetDate 重置每日挑战数据===========');
        let ugList = await redisBase.getGameTzInfoList();
        for (let i = 0; i < ugList.length ; i++) {
            let ugInfo = JSON.parse(ugList[i]);
            ugInfo.addTzCountCur = 0; //免费看视屏添加挑战次数
            ugInfo.curFuHuo = 0; //看视频复活次数
            ugInfo.tzCountDefault = this.lastCount; //每日默认挑战次数
            //更新挑战列表数据
            await redisBase.updateGameTzInfoList(i,JSON.stringify(ugInfo));
        }
    }

    async saveGameCfgTds(){
        let list = await redisBase.getGameTzCfgList();
        if(list && list.length <= 0){
            let types = [gameCfg.DALIYCHAType.DC_XX,gameCfg.DALIYCHAType.DC_DS]
            for (let i = 0; i < this.gameCfg.length ; i++) {
                let cfg =this.gameCfg[i];
                let item = {
                    gtype:types[i],
                    total: cfg.total,
                    rewards: cfg.rewards
                }
                await redisBase.addGameTzCfgToList(item);
            }
        }
    }

    static getInstance(){
        if(!HallTz.instance){
            HallTz.instance = new HallTz();
            return HallTz.instance;
        }else{
            return HallTz.instance;
        }
    }

    async mrtz_game_cfg(data, ctype,client){
        let uid = data.userId;

        let userZt = await redisBase.getGametzInfoByUid(uid);
        if(!userZt){
            let user = new PlayerInfoZt(uid,-1);
            await redisBase.addGameTzInfoToList(user);
        }
        userZt = await redisBase.getGametzInfoByUid(uid);

        let user = await redisBase.getUserInfo(uid);
        let ret ={
            type: ctype,
            data:{
                code : Respones.OK, // 0成功 1失败
                //今日剩余挑战次数
                count: userZt.tzCountDefault,
                //剩余抽奖券
                ticket : user.ticket,
                //休闲挑战消耗
                costXX : this.costXX,
                //大神挑战消耗
                costDS : this.costDS,
                //当日看视频获取获取挑战次数是否使用完毕 1 是正常， 2 表示次数已经使用完毕
                status : (userZt.addTzCountMax - userZt.addTzCountCur) <=0  ? 2 : 1,
            }
        }

        //返回结果
        client.send(JSON.stringify(ret));

        //将玩法选择的配置保存到redis
        await this.saveGameCfgTds();
        //end
    }

    //每日挑战选择
    async mrtz_game_choice(data, ctype,client){
        let uid = data.userId;
        let dcType = data.dcType;

        let ret ={
            type: ctype,
            data: {
                dcType: dcType,
                topicTotal: 0,
                listReward:[],
            }
        }

        if(dcType === gameCfg.DALIYCHAType.DC_XX){
            ret.data.topicTotal = this.gameCfg[0].total;
            ret.data.listReward= this.gameCfg[0].rewards;
        }else if(dcType === gameCfg.DALIYCHAType.DC_DS){
            ret.data.topicTotal = this.gameCfg[1].total;
            ret.data.listReward= this.gameCfg[1].rewards;
        }

        //返回数据
        client.send(JSON.stringify(ret));

        //保存我选择的到redis
        let list = await redisBase.getGameTzInfoList();
        for (let i = 0; i < list.length; i++) {
            let cf = JSON.parse(list[i]);
            if(cf.uid === uid){
                cf.gType = dcType;
                await redisBase.updateGameTzInfoList(i,JSON.stringify(cf));
                break;
            }
        }
    }

    async mrtz_game_start(data, ctype,client){
        let uid = data.userId;
        let dcType = data.dcType;

        let ret ={
            type: ctype,
            data: {
                code: Respones.OK
            }
        }
        //扣费逻辑
        let ugList = await redisBase.getGameTzInfoList();
        for (let i = 0; i < ugList.length ; i++) {
            let ugInfo = JSON.parse(ugList[i]);
            if(ugInfo.uid === uid){

                if(ugInfo.tzCountDefault > 0){
                    let gType = ugInfo.gType;
                    //判断消耗,用户列表
                    let uList = await redisBase.getUserList();
                    for (let j = 0; j < uList.length; j++) {
                        let user = JSON.parse(uList[j]);
                        if(user.userId === uid){
                            if(gType === DALIYCHAType.DC_DS){
                                if(user.ballNum >= this.costDS){
                                    user.ballNum = user.ballNum*1 - this.costDS;
                                    ugInfo.tzCountDefault--
                                }else{
                                    ret.data.code = Respones.QIU_NAN; //球币不足
                                }
                            }else if(gType === DALIYCHAType.DC_XX){
                                user.ballNum = user.ballNum*1 - this.costXX;
                                ugInfo.tzCountDefault--;
                            }
                            //更新用户数据列表
                            await redisBase.updateUserist(j,JSON.stringify(user));
                            break;
                        }
                    }
                }else if(ugInfo.tzCountDefault <= 0){ //挑战次数不足
                    ret.data.code = Respones.COUNT_TZ_NAN; //挑战次数不足
                }
                //更新挑战列表数据
                await redisBase.updateGameTzInfoList(i,JSON.stringify(ugInfo));
                break;
            }
        }

        log.info('======mrtz_game_start end==========');
        client.send(JSON.stringify(ret));
    }

    async mrtz_game_add_count(data, ctype,client){
        let uid = data.userId;

        let ret ={
            type: ctype,
            data: {
                code: Respones.INVALID,
                count:0 ,
                status:0 //1 是正常， 2 表示次数已经使用完毕
            }
        }

        let ugList = await redisBase.getGameTzInfoList();
        for (let i = 0; i < ugList.length ; i++) {
            let ugInfo = JSON.parse(ugList[i]);
            if(ugInfo.uid === uid){
                if(ugInfo.addTzCountCur < ugInfo.addTzCountMax){
                    ugInfo.addTzCountCur++;
                    ugInfo.tzCountDefault++;
                    ret.data.count = ugInfo.tzCountDefault;
                    ret.data.code = Respones.OK;
                }

                if(ugInfo.addTzCountCur < ugInfo.addTzCountMax){
                    ret.data.status = 1;
                }else{
                    ret.data.status = 2;
                }
                //更新挑战列表数据
                await redisBase.updateGameTzInfoList(i,JSON.stringify(ugInfo));
                break;
            }
        }

        log.info('=====mrtz_game_add_count end==============',ret);
        client.send(JSON.stringify(ret));
    }
}

module.exports = HallTz.getInstance();